package org.sdau.rotten.library.controller;

import cn.hutool.core.date.DateField;
import cn.hutool.core.date.DateUtil;
import org.sdau.rotten.library.entity.Action;
import org.sdau.rotten.library.service.AppointService;
import org.sdau.rotten.library.DTO.JsonData;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.servlet.ModelAndView;

import javax.annotation.Resource;
import java.util.Date;
import java.util.List;

@Controller
@RequestMapping("/customer/adminOperation")
public class AdminOperationController {
    @Resource(name = "appointmentServiceImpl")
    private AppointService appointService;
    @GetMapping("/listAction")
    public ModelAndView listAction(ModelAndView model, @RequestParam("userId") String userId) {
        List<Action> list = appointService.findAll(userId);
        list.forEach(item->{
            Date newDate = DateUtil.offset(item.getAppointTime(), DateField.DAY_OF_WEEK, 14);
            System.out.println(newDate);
            item.setBackTime(newDate);
        });
        model.addObject("list", list);
        model.setViewName("borrow_situation");

        return model;
    }

    @GetMapping("/delete")
    public JsonData deleteAction( @RequestParam("Id") String Id) {
        System.out.println(Id);
        int back = appointService.delete(Id);
        if(back>=0)
            return JsonData.buildSuccess(Id+"删除成功");
        return JsonData.buildError(Id+"删除失败");
    }
}
